<template>
	<view class="content">
		<view class="wraper">
			<view class="box flex mtb-12" v-for="(va,ind) in msgList" @click="tomsg(va._id)" :key="ind">
				<block v-for="(item,index) in va.user " :key="index">
					<block v-if="item.openid != openid">
						<view class="avater">
							<image :src="item.avater" mode=""></image>
						</view>
						<view class="namebox w-50 flex col bt">
							<view class="name">
								{{item.name}}
							</view>
							<view class="msg" v-if="item.lastmessage">
								{{item.lastmessage}}
							</view>
						</view>
						<view class="date" v-if="va.date">
							{{va.date}}
						</view>
					</block>
				</block>
			</view>
		</view>
		<button type="button" @click="createMsg">创建会话</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				msgList: [], // 会话list
				openid: ""
			}
		},
		onLoad() {
			this.openid = uni.getStorageSync("openid")
			console.log(this.openid)
			this.getSession()
		},
		methods: {
			// 进入聊天
			tomsg(i) {
				console.log(i)
				uni.navigateTo({
					url: '/subpack/message/message?id=' + i
				})
			},
			// 获取会话列表
			async getSession() {
				let res = await wx.cloud.callFunction({
					name: 'logins',
					data: {
						useName: 'getSessionList'
					}
				})
				this.msgList = res.result.data
				console.log("res:", res)
			},
			// 创建会话、发起聊天
			async createSe() {
				let res = await wx.cloud.callFunction({
					name: 'logins',
					data: {
						useName: 'createSession',
						user: [{
							avater: "cloud://animal-4g5ks6ct0e7f251e.616e-animal-4g5ks6ct0e7f251e-1315698572/avater/zmihzIhnOYma6604861c85a691ff3e954540831baa26.jpeg",
							name: "1.",
							openid: "oBiar5NSgldQsAPtf8wW6cvkw3wM"
						}, {
							avater: "https://616e-animal-4g5ks6ct0e7f251e-1315698572.tcb.qcloud.la/7b63f2f6-64a4-3bff-89c9-4606c08a37ca!nd_whgt34_webp_wm_1.jpg?sign=a524732d9122c5d975592cf8a4be3cc9&t=1721957391",
							name: "狗",
							openid: "oBiar5NSgldQsAPtf8wW6cvkw3wM2"
						}],
						date:this.$formatDateTime(new Date()),
					}
				})
				console.log("res:", res)
			},
			// 创建消息
			async createMsg() {
				let res = await wx.cloud.callFunction({
					name: 'logins',
					data: {
						useName: 'createMsg',
						content: {
							avater: "cloud://animal-4g5ks6ct0e7f251e.616e-animal-4g5ks6ct0e7f251e-1315698572/avater/zmihzIhnOYma6604861c85a691ff3e954540831baa26.jpeg",
							image: "", // 聊天图片
							date:this.$formatDateTime(new Date(), true),
							text: "如果你要买，hahahah！" // 内容
						},
						receiver: "oBiar5NSgldQsAPtf8wW6cvkw3wM2",
						session_id: "2ad666ce66ad9a530580470472fe5506"
					}
				})
				console.log("MSG:", res)
			}
		}
	}
</script>

<style lang="scss" scoped>
	.content {
		min-height: 100vh;
		background: linear-gradient(#fff, #949494);

		.wraper {
			padding: $uni-spacing-row-base;

			.box {
				width: 100%;
				border: 1px solid #000;
				border-radius: 14rpx;
				max-height: 400rpx;
				min-height: 200rpx;
				background: #fff;

				.avater {
					width: 120rpx;
					height: 120rpx;
					margin-top: 40rpx;
					margin-left: 40rpx;

					image {
						width: 100%;
						height: 100%;
						border-radius: 50%;
					}
				}

				.namebox {
					margin: 34rpx 10rpx;
					overflow: hidden;

					// text-overflow: ellipsis;
					.name {
						font-size: 44rpx;
						color: #000;
					}

					.msg {
						color: gray;
					}
				}

				.date {
					font-size: 24rpx;
					padding-left: 20rpx;
					padding-top: 20rpx;
				}
			}
		}
	}
</style>